//
//  LogVC.m
//  UHealth
//
//  Created by gjtxz on 15/2/28.
//  Copyright (c) 2015年 gjtx. All rights reserved.
//

#import "LogVC.h"
#import "TimeLineViewControl.h"

@interface LogVC ()

@end

@implementation LogVC

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//        self.view.backgroundColor = kBacColor;
//        self.navigationItem.title = kLocal(@"日志");
//        [self setHidesBottomBarWhenPushed:YES];
//        [self.navigationController.navigationBar setBarTintColor:kBlueColor];
//
//    }
//    return self;
//}
-(id)init{
    self = [super init];
    if (self) {
        self.view.backgroundColor = kBacColor;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NavView * nav = [[NavView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, kNavHeight) andTitleStr:kLocal(@"log") andVC:self];
    [self.view addSubview:nav];

    NSArray *dbArr;
    NSString *sensorID = kLastBindDevice;//默认针头
    if(sensorID){
        
        // 只去今天的日志
        NSString *dayStartStr = [NSString stringWithFormat:@"%@ 00:00:00",[[NSString stringFromDate:[NSDate date]] substringToIndex:10]];
        NSString *dayEndStr = [NSString stringWithFormat:@"%@ 00:00:00",[[NSString stringFromDate:[NSDate dateWithTimeInterval:(24*3600) sinceDate:[NSDate date]]] substringToIndex:10]];
        
        NSString *condition = [NSString stringWithFormat:@"%@ = '%@' AND %@ > datetime('%@') and %@ < datetime('%@')",kDeviceID,sensorID, kLogDate, dayStartStr, kLogDate, dayEndStr];
        dbArr = [[DBManage sharedManager] queryDataFromTable:kLogTable andConditions:condition];//默认针头日志
    }else{
        dbArr = [NSArray array];
    }
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_left_top_back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    

    if (dbArr.count) {
        NSMutableArray *timeArr = [NSMutableArray array];
        NSMutableArray *DesArr = [NSMutableArray array];
        for (LogDataObj *log in dbArr){
            NSString *str = @"";
            switch (log.logType) {
                case BLELogTypeDisconnected:
                    str = kLocal(@"disconnect");
                    break;
                case BLELogTypeConnected:
                    str = kLocal(@"connect_successful");
                    break;
                case BLELogTypeLowPower:
                    str = kLocal(@"low_voltage");
                    break;
                case BLELogTypeHeightTemperature:
                    str = kLocal(@"tem_too_high");
                    break;
            }
            [timeArr addObject:log.logDate];
            [DesArr addObject:str];
        }
        TimeLineViewControl *timeline = [[TimeLineViewControl alloc] initWithTimeArray:timeArr
                                                               andTimeDescriptionArray:DesArr
                                                                      andCurrentStatus:1
                                                                              andFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kNavHeight)];
        
        timeline.center = self.view.center;
//        timeline.backgroundColor = kBlueColor;
        
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavHeight, kScreenWidth, kScreenHeight-kNavHeight)];
        scrollView.contentSize = CGSizeMake(0, timeline.viewheight+kNavHeight*2);
        [scrollView addSubview:timeline];
        [self.view addSubview:scrollView];
    }
}

- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
